#include "EpollReactor.hpp"
#include "Common.hpp"
#include "Calculator.hpp"
#include "Protocol.hpp"

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        LOG(LogLevel::FATAL) << "Usage: " << argv[0] << " + port";
        return USAGE_ERROR;
    }
    
    in_port_t port = std::stoi(argv[1]);
    
    // 顶层服务: 计算器
    Calculator calculator;

    // 协议层
    Protocol protocol([&calculator](const Request& request)->Response{
        return calculator.Calculate(request);
    });

    // 协议层回调函数注册到listen套接字事件处理器listen_eh
    // 该回调函数会被listen_eh注册到每个连接套接字的处理器
    std::shared_ptr<ListenSocketEH> listen_eh = std::make_shared<ListenSocketEH>(port, [&protocol](std::string& buffer){
        return protocol.ServerService(buffer);
    });

    // 创建reactor服务
    std::unique_ptr<EpollReactor> reactor = std::make_unique<EpollReactor>(256);
    // 把listen_eh注册到reactor服务中
    reactor->RegisterEH(listen_eh, listen_eh->Events());
    // 启动reactor服务
    reactor->Run(10000);
    return 0;
}